<?php 
include MODULE_ROOT.'/inc/mobile/common/global.func.php';
global $_W,$_GPC;

$id = intval($_GPC['id']);

$sql = "SELECT * FROM ".tablename('imeepos_runner3_tasks')." WHERE id = :id";
$params = array(':id'=>$id);
$task = pdo_fetch($sql,$params);
load()->model('mc');

$runner = mc_fetch($_W['openid']);

if(empty($task) || $task['status'] != 1){
	$return = array();
	$return['success'] = 1;
	$return['message'] = '您来晚了，订单已被抢！';
	die(json_encode($return));
}else{
	$data = array();
	$data['openid'] = $_W['openid'];
	$data['uniacid'] = $_W['uniacid'];
	$data['taskid'] = $id;
	$data['create_time'] = time();
	
	pdo_insert('imeepos_runner3_recive',$data);
	$reciveid = pdo_insertid();
	pdo_update('imeepos_runner3_tasks',array('status'=>2),array('id'=>$id));
	//发送消息
	$content = "";
	$content = "恭喜您，您的任务已被".$runner['nickname']."接单，请注意任务动态~\n";
	$content .= "时间：".date('Y年m月d日 h点i分')."\n";
	$content .= "咚咚咚，您的任务已被受理".$task['code']."，请注意保存，不要泄露~点击详情查看详情";
	$url = $_W['siteroot'].'app/'.$this->createMobileUrl('manage',array('id'=>$task['id']));
	mc_notice_consume2($task['openid'], '您的任务已被受理提醒', $content, $url,'');
	
	$return = array();
	$return['success'] = 0;
	$return['message'] = '恭喜您，抢单成功！';
	$return['id'] = $reciveid;
	die(json_encode($return));
}
